js.underas.controller.ListViewItemRender = new Class({
	"Extends":js.underas.core.Component
	,"_maxCells":0
	,"_maxRows":5
	,"initialize":function(p_ele,p_innerhtml){
		this.parent(p_ele,p_innerhtml);
	}
	,"getMaxCells":function(){
		if(this._maxCells==0){
			var tmsA = this.getEle().get("class").match(/col\-sm\-(\d{1,2})/);
			if(tmsA){
				this._maxCells = 12/parseInt(tmsA[1]);
			}
		}
		return this._maxCells;
	}
	,"getMaxRows":function(){
		return this._maxRows;
	}
	,"setMaxCells":function(p_maxcells){
		this._maxCells=p_maxcells;
	}
	,"setMaxRows":function(p_maxrows){
		this._maxRows=p_maxrows;
	}
});

var DefaultItemRender = new Class({
	"Extends":js.underas.controller.ListViewItemRender
	,"initialize":function(p_obj){		
		var htmlTmp = "";		
		var count = 0;
		Object.each(p_obj,function(value,key){
			count++;
			if(count==1){				
				htmlTmp += '<h4>'+value;
			}else if(count==2){
				htmlTmp += '-'+value+'</h4>';
			}else if(key!="_ind"){				
				htmlTmp += '<p class="list-group-item-text">'+key.replace(/([a-z]{2})([A-Z])/,"$2").toLowerCase()+':'+value+"</p>";
			}			
		});	
		this.parent('div',htmlTmp);
		this.getEle().addClass("col-xs-12 col-sm-6 col-md-4");
	}
});
/**
 * @class js.underas.controller.ListView
 * @param {String} p_title titulo do list view
 * @extends js.underas.core.Component
 */
js.underas.controller.ListView = new Class({
/**@lends js.underas.controller.ListView#*/
	"Extends":js.underas.core.Component
	,"_item_render":"DefaultItemRender"
	,"dataProvider":null
	,"_ind":0
	,"_itFilter":null
	,"_itOrderBy":null
	,"itemChange":null
	,"_varmod":null
	,"_islistview":true
	,"_rowhtml":'<div><div class="col-sm-12 col-sx-12"></div></div>'
	,"initialize":function(p_title){
		this.parent("div",'<div class="headgridview col-xs-12 col-sm-12 col-md-12"></div><div class="tilecellgrid col-xs-12 col-sm-12 col-md-12"></div>');
		this._islistview = true;
		this.dataProvider = new js.underas.util.ArrayList([]);	
		this.getEle().addClass("boxTileLine ListView");
		
		this.getEle().appendHTML('<div class="col-xs-12"><ul id="pagination_'+this.uid+'" class="pagination pagination-sm"></ul></div>');
		
		this._itFilter = new js.underas.controller.InputText("");
		this._itFilter.setSize(12);
		this._itFilter.setLabel("filtrar");
		this._itFilter.setIcon("search");
		//this._itFilter.setIcon("refresh",2);
		//this._itFilter.addEvent("keyup",this.setFilter.bind(this));		
		this._itFilter.addEvent('keydown',this.setFilter.bind(this));
		
		this._itOrderBy = new js.underas.controller.Select("");
		this._itOrderBy.setSize(5);
		this._itOrderBy.setLabel("ordem");
		this._itOrderBy.setIcon("chevron-down");		
		this._itOrderBy.setValueField("idColumn");
		this._itOrderBy.setLabelField("column");
		//this._itOrderBy.getInput().addEvent("change",this.setOrder.bind(this));
		this._itOrderBy.getInput().set("data-orderfield","asc");
		//this._itOrderBy.getEle(".addon1").addEvent("click",this.setOrderField.bind(this));		
		//this.getEle().grab(this._itOrderBy.getEle(),"top");
		this.getEle().grab(this._itFilter.getEle(),"top");	
		//this.append(this._itSearch);
		//this.itemChange = function(p_item){};		
		this.getEle(".tilecellgrid").addEvent('click:relay(.tilecell)',this.changeSelectedItem.bind(this));	
		this.getEle('#pagination_'+this.uid).addEvent('click:relay(li a)',this.changePg.bind(this));
	}
	/**
	* alterar o nome da variavel do modulo.
	* @memberOf js.underas.controller.ListView#
	* @param {String} p_varmod
	*/	
	,"setVarModule":function(p_varmod){
		this.getEle().set("data-varmod",p_varmod);
		this._varmod=p_varmod;
	}
	,"getVarModule":function(){
		return this._varmod;
	}
	,"getModule":function(){		
		return window[this.getVarModule()];
	}
	,"setItemRender":function(p_itemrender){
		this._item_render = p_itemrender;
	}
	/**
	Returns the sum of all numbers passed to the function.
	@param {...string} num A positive or negative number
	*/
	,"getItemRender":function(){
		return this._item_render;
	}
	,"setDataProvider":function(p_dta){		
		if(p_dta){
			this.dataProvider.addAll(p_dta);			
			if(this.dataProvider.size() > 0){
				var tmpColumns = this.getModule().getColumns();
				var dtaOrder = [];
				tmpColumns.each(function(cfield,index){
					var tcolumn = cfield["column"];
					var indN = dtaOrder.length;
					//tcolumn.replace(/([a-z]{2})([A-Z])/
					dtaOrder[indN] = {"idColumn":tcolumn,"column":tcolumn};			
				});	
				this._itOrderBy.setDataProvider(dtaOrder);		
			}					
		}else{
			this.dataProvider.removeAll();
		}
		
		return this.refresh();
	}
	,"getDataProvider":function(){
		return this.dataProvider;
	}
	,"setHeight":function(p_height){
		this.getEle().setStyles({"height":p_height+"px"});
	}
	,"setRow":function(p_html){		
		this._rowhtml = p_html;
	}
	,"addRow":function(p_pg,p_html){		
		var tmpRow = null;
		if(p_html){
			tmpRow = Elements.from(p_html);
		}else{
			tmpRow = Elements.from(this._rowhtml); // new Element('div',{'class':'row_cells','html':this._rowhtml});	
		}
		tmpRow.set("data-citens",0).addClass('row_cells col pg_u'+p_pg);
		tmpRow.inject(this.getEle(".tilecellgrid"));
		//this.getEle(".tilecellgrid").grab(tmpRow);
	}
	,"setHeadGrid":function(p_html){	
		this.getEle(".headgridview").set("html",p_html);
		//Elements.from(p_html).inject(this.getEle(".headgridview"));	
		//.grab(,"top");	
	}
	,"getRow":function(p_index){
		return this.getEle(".tilecellgrid").getLast(".row_cells");
	}
	,"clear":function(){
		this.getEle(".tilecellgrid").set("html","");
		$('pagination_'+this.uid).set("html","");
	}
	,"refresh":function(){
		var dpt = this.dataProvider.size();
		this.clear();
		if(dpt > 0){				
			var qt_pg = 1;
			var qt_row = 0;
			var qt_cells = 0;
			varpgvisible = "block";	
			var tmpItemRender =  new window[this.getItemRender()](this.dataProvider.get(0));						
			var max_cells = tmpItemRender.getMaxCells();
			var max_rows = tmpItemRender.getMaxRows();
			tmpItemRender = null;		
			$('pagination_'+this.uid).grab(new Element('li',{'html':'<a class="active" data-pg="'+qt_pg+'" href="#">'+qt_pg+'</a>'}));			
			this.addRow(qt_pg);
			this.getRow().setStyle("display",varpgvisible);			
			for(var z=0;z<dpt;z++){			
				if(qt_cells >= max_cells){
					qt_cells=0;
					qt_row++;				
					if(qt_row >= max_rows){
						qt_row=0;
						qt_pg++;	
						varpgvisible = "none";	
						$('pagination_'+this.uid).grab(new Element('li',{'html':'<a class="disabled" data-pg="'+qt_pg+'" href="#">'+qt_pg+'</a>'}));					
					};					
					this.addRow(qt_pg);
					this.getRow().setStyle("display",varpgvisible);
				};				
				qt_cells++;
				this.getRow().set("data-citens",qt_cells);				
				var item  =  this.dataProvider.get(z);
				item["_ind"]=z;
				var itemRender = {};
				itemRender = new window[this.getItemRender()](item);
				itemRender.getEle().set("data-ind",z).addClass("tilecell list-group-item "+this.getItemRender());
				this.getRow().grab(itemRender.getEle(),'bottom');								
			}; 
		};
		return this;
	}
	,"setFilter":function(evt){		
		if(evt.key == "enter"){	
			//console.log("e:"+evt.key);		
			var p_filter = evt.target.get("value").toLowerCase();	
			//console.log(p_filter);	
			//if(p_filter.length > 3){			
				this.getEle(".tilecellgrid").getChildren(".row_cells").each(function(item){				
					if(item.get("text").toLowerCase().indexOf(p_filter)>-1){
						item.setStyle("display","block");					
						item.getChildren(".tilecell").each(function(item2){				
							if(item2.get("text").toLowerCase().indexOf(p_filter)>-1){							
								item2.setStyle("display","block");
							}else{
								item2.setStyle("display","none");
							}
						});	
					}else{
						item.setStyle("display","none");
					}	
				});
			//}				
		}		
	}	
	,"setOrderField":function(evt){
		var oldOrder = this._itOrderBy.getInput().get("data-orderfield");
		var icontmp = "chevron-down";
		if(oldOrder=="asc"){
			oldOrder="desc";
			icontmp = "chevron-up";
			this.orderDesc(this._itOrderBy.getValue());
		}else{
			oldOrder="asc";
			icontmp = "chevron-down";
			this.orderAsc(this._itOrderBy.getValue());
		};
		this._itOrderBy.getInput().set("data-orderfield",oldOrder);
		this._itOrderBy.setIcon(icontmp);
		
	}
	,"setOrder":function(evt){
		this.orderAsc(this._itOrderBy.getValue());	
	}
	,"orderDesc":function(p_campo){
		this.dataProvider.orderDesc(p_campo);
		this.refreshGridDate();
	}
	,"orderAsc":function(p_campo){
		this.dataProvider.orderAsc(p_campo);
		this.refreshGridDate();	
	}
	,"getSelectedIndex":function(){
        return this._ind;
	}
	,"getSelectedItem":function(){
        if(this.dataProvider.size() < this._ind){
            this._ind = 0;
        };
        return this.dataProvider.get(this._ind);
	}
	,"setSelectedItem":function(p_item){
		this.dataProvider.set(p_item,this._ind);
	}
	,"setSelectedIndex":function(p_index){
		 this._ind = p_index;
	}
	,"changeToIndex":function(p_index){	
		if(this.getDataProvider().size()>p_index){
			this.setSelectedIndex(p_index);
			this.getEle(".tilecellgrid").getChildren(".row_cells .selectedLine").each(function(item){
				item.removeClass("selectedLine").removeClass("active");
			});	
			if(this.getSelectedItem()){
				this.getEle(".tilecellgrid").getChildren(".row_cells .tilecell")[p_index].addClass("selectedLine active");
			};
			if(this.itemChange&&this.getSelectedItem()){
				this.getModule().onChangeItem(this.getSelectedItem());
				this.itemChange(this.getSelectedItem());			
			};
		}else{
			if(this.getModule()){
				this.getModule().clearFormItem();
			};
			if(this.itemChange){
				this.itemChange();
			};
		};		       
	}
	,"updateItem":function(p_item){
		this.setSelectedItem(p_item);
		var itemRender = {};
		itemRender = new window[this.getItemRender()](p_item);
		itemRender.getEle().set("data-ind",this.getSelectedIndex()).addClass("tilecell list-group-item "+this.getItemRender());
		this.getEle().getChildren(".tilecellgrid .selectedLine").grab(itemRender.getEle(),'before');
		this.getEle().getChildren(".tilecellgrid .selectedLine").destroy();
		itemRender.getEle().addClass("selectedLine");
	}
	,"insertItem":function(p_item,p_where){
		var lwhere = p_where||'top';
		//this.setSelectedItem(p_item);
		var nind = this.dataProvider.size();		
		this.dataProvider.add(p_item);
		this._ind = nind;
		var itemRender = {};
		p_item["_ind"]=nind;
		itemRender = new window[this.getItemRender()](p_item);
		//var uidItem = itemRender.getEle().get("id");
		itemRender.getEle().set("data-ind",nind).addClass("tilecell selectedLine list-group-item "+this.getItemRender());		
		var tcitens = 1;
		if(nind==0){
			this.addRow(1);
			this.getRow().setStyle("display","block");
		}else{
			var maxCells = itemRender.getMaxCells();
			var tmpCitens = maxCells+"";
			if(this.getRow()){
				tmpCitens = this.getRow().get("data-citens");
			};
			var tcitens = parseInt(tmpCitens);
			if(tcitens==maxCells){
				this.addRow(1);
				this.getRow().setStyle("display","block");
				tcitens=0;
			};
			tcitens++;
		};
		this.getRow().set("data-citens",tcitens).grab(itemRender.getEle(),lwhere);
		this.changeSelectedItem(null,itemRender.getEle());
	}
	,"removeSelectedItem":function(){
		//this.setSelectedItem();
		//nao remover do array para evitar null point
		this.getEle().getChildren(".tilecellgrid .selectedLine").destroy();
	}
	,"removeItem":function(p_item){
		this.removeSelectedItem();
	}	
	,"changePg":function(evt,tgt){		
		evt.preventDefault();
		//tgt.addClass("selectedLine active");		
		$$('#pagination_'+this.uid+' li a.active').removeClass("active").addClass("disabled");
		tgt.addClass("active");		
		this.getEle(".tilecellgrid").getChildren(".row_cells").each(function(item){
			item.setStyle("display","none");
		});	
		
		this.getEle(".tilecellgrid").getChildren('.pg_u'+tgt.get("data-pg")).each(function(item){
			item.setStyle("display","block");
			item.getChildren(".tilecell").each(function(item2){item2.setStyle("display","block");});
		});	
		/*
		$$('#pagination_'+this.uid+' .tilecellgrid .tilecell').setStyle("display","none");
		$$('#pagination_'+this.uid+' .tilecellgrid .pg_u'+tgt.get("data-pg")).setStyle("display","block");
		*/
		
		return false;
				       
	}
	,"changeSelectedItem":function(evt,tgt){		
		this.getEle(".tilecellgrid").getChildren(".row_cells .selectedLine").each(function(item){
			item.removeClass("selectedLine").removeClass("active");
		});		
		tgt.addClass("selectedLine active");		
		this["_ind"] = parseInt(tgt.get("data-ind"));		
		if(this.itemChange&&this.getSelectedItem()){
			this.itemChange(this.getSelectedItem());			
		};
		if(this.getSelectedItem()){
			this.getModule().onChangeItem(this.getSelectedItem());
		};
	}
});
